extends "res://ui/msgBaseX/msgBaseX.gd"

onready  var grid = $Panel / ScrollContainer / GridContainer
var player:Player = null
var utils = globalData.infoDs["g_azurlaneUtils"]
var azurCtrl = globalData.infoDs["g_azurlane"]
var dock = globalData.infoDs["g_azurlaneDock"]
var shopItemScript = load(self.get_script().get_path().get_base_dir() + "/azurShopItem.gd")
func _ready():
	popup()
		
func init(player:Player):
	up(player)
	player.connect("onChangeGold", self, "_onChangeGold")
	_onChangeGold()
	if sys.adSdk == null:
		$Panel / Button2.hide()
	else :
		sys.adSdk.connect("onError", self, "onError")
		sys.adSdk.connect("onRewardVerify", self, "onRewardVerify")
	utils.createItemButton($Panel, self, "售卖角色", "sellCharas", [], Vector2(750, 610))	

	grid.columns = 6
	grid.rect_position.x -= 100
		
func up(player):
	self.player = player
	for i in range(4):
		var id = itemData.rndGet()
		addItem(id, 1, 200)
	for i in range(3):
		var id = sys.rndListItem(itemData.rares)
		addItem(id, 1, 300)
	
	addItem("mf", 3, 256, "200魔方")
	addItem("buli", 3, 400, "3布里")
	
func addItem(id, type = 1, p = null, text = ""):
	var item = preload("res://ui/shopMsg/shopItem.tscn").instance()
	changeItemScript(item, shopItemScript)
	grid.add_child(item)
	item.init(id, type, p, text)
	item.connect("onPressed", self, "mai")

func changeItemScript(node, script):
	var prev_variables = {}
	var skip_variables = ["Script Variables", "script", "rect_global_position"]
	for property in node.get_property_list():
		var key = property.name
		if not key in skip_variables and key != "":
			prev_variables[key] = node.get(key)
	node.set_script(script)
	for property in prev_variables:
		node.set(property, prev_variables[property])	
	
func _onChangeGold():
	$Panel / Label.text = "%dG" % player.gold

func rndCha1(obj):
	if obj.lv == 1:return true
func rndCha2(obj):
	if obj.lv == 2:return true
func rndCha3(obj):
	if obj.lv == 3:return true
	
func onRewardVerify():
	for i in $Panel / ScrollContainer / GridContainer.get_children():
		i.free()
	yield (get_tree().create_timer(0.1), "timeout")
	up(player)
	
func onError():
	$Panel / Button2.show()

func mai(item):
	if player.subGold(item.price):
		match item.type:
			1:
				player.addItem(item.info)
			2:
				item.get_node("Control").remove_child(item.info)
				player.addCha(item.info)

			3:
				match item.info:
					"mf":azurCtrl.changeMF(200)
					"buli":azurCtrl.changeBuli(3)

		item.queue_free()
	else:
		sys.newBaseMsg("信息", "金币不足")

func _on_Button_pressed():
	queue_free()

func _on_Button2_pressed():
	if sys.adPlay():
		$Panel / Button2.hide()
		

func sellCharas():
	if not $Panel.has_node("sellWindow"):
		buildSellChas()
	$Panel.get_node("sellWindow").popup_centered()
	
func buildSellChas():
	var sellWindow = utils.createWindowDialog($Panel, "售卖角色", Vector2(750, 350))
	sellWindow.name = "sellWindow"
	var vbox = utils.createVBox(sellWindow)
	vbox.rect_position = Vector2(30, 30)
	utils.createRichTextLabel("批量售卖备战栏对应等级的[color=#DC143C]未装备任何道具[/color]的角色，返还80%建造资源", vbox, Vector2(600, 50))
	utils.createItemButton(vbox, self, "一级角色", "doSellChas", [1])
	utils.createItemButton(vbox, self, "二级角色", "doSellChas", [2])
	utils.createItemButton(vbox, self, "三级角色", "doSellChas", [3])
	utils.createItemButton(vbox, self, "四级角色", "doSellChas", [4])

func doSellChas(lv):
	for i in utils.getReadyChas():
		if i.lv == lv and i.items.empty() and i.type != sys.CharaClass.NPC:
			var cost = dock.cost1
			if i.type == sys.CharaClass.CL or i.type == sys.CharaClass.CA:
				cost = dock.cost2
			elif i.type == sys.CharaClass.BB:
				cost = dock.cost3
			elif i.type == sys.CharaClass.CV or i.type == sys.CharaClass.CVL:
				cost = dock.cost4
			
			azurCtrl.changeMF(cost*0.8)
			utils.deleteCha(i)

